import numpy as np

def softmax_function1(a):  #定义softmax函数
    exp_a = np.exp(a)
    sum_exp_a = np.sum(exp_a)
    y = exp_a / sum_exp_a

    return y

def softmax_function2(a):  #定义softmax函数，添加溢出对策
    c = np.max(a)
    exp_a = np.exp(a - c)
    sum_exp_a = np.sum(exp_a)
    y = exp_a / sum_exp_a

    return y

x1 = np.array([0.3, 2.9, 4.0])
y1 = softmax_function1(x1)
print(y1)
print(np.sum(y1))

x1 = np.array([0.3, 2.9, 4.0])
y1 = softmax_function2(x1)
print(y1)
print(np.sum(y1))

x2 = np.array([1010, 1000, 990])
y2 = softmax_function1(x2)
print(y2)

x3 = np.array([1010, 1000, 990])
y3 = softmax_function2(x3)
print(y3)
print(np.sum(y3))
